import { formatDate } from '@/utils'
import { Notify } from 'vant'

export default {
    namespaced: true,
    state: {
        messageList: [],
        position: 0  // 数据开始位置
    },
    mutations: {
        // 一对一聊天
        SOCKET_private_chat(state, data) {
            data.time = formatDate(data.time)
            // msgType 消息类型（0:文字（包括表情包），1:图片）
            if (data.msgType === 0) {
                // 文字
                state.position = 1
                state.messageList.push(data)
            } else {
                if (data.message instanceof Array) {
                    const { userId, message, msgType, time, fromId, toId } = data
                    // 多图片
                    for (let i = 0; i < data.message.length; i++) {
                        let newData = {
                            userId,
                            msgType,
                            message: message[i],
                            time,
                            fromId,
                            toId
                        }
                        state.position = data.message.length
                        state.messageList.push(newData)
                    }
                } else {
                    // 单图片
                    state.position = 1
                    state.messageList.push(data)
                }
            }
        },
        // 群聊
        SOCKET_group_chat(state, data) {
            data.time = formatDate(data.time)
            if (data.msgType === 0) {
                // 文字
                state.position = 1
                state.messageList.push(data)
            } else {
                if (data.message instanceof Array) {
                    const { userId, message, msgType, time, fromId, toId } = data
                    // 多图片
                    for (let i = 0; i < data.message.length; i++) {
                        let newData = {
                            userId,
                            msgType,
                            message: message[i],
                            time,
                            fromId,
                            toId
                        }
                        state.position = data.message.length
                        state.messageList.push(newData)
                    }
                } else {
                    // 单图片
                    state.position = 1
                    state.messageList.push(data)
                }
            }
        },
        // 登录
        SOCKET_login(state, data) {
            let userInfo = localStorage.getItem('userInfo')
            Notify({ type: 'success', message: `${JSON.parse(userInfo).nick}已上线` })
        }
    },
    actions: {
    },
    getters: {
    }
}